<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.net.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>cookie</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
	<p>保存写入cookie</p>
	<%
	
		String name = URLEncoder.encode("张三welcome to chengdu", "utf-8");
		Cookie nameCookie = new Cookie("username",name); //创建一个cookie
		Cookie pswCookie = new Cookie("password","123"); //创建一个cookie
		nameCookie.setMaxAge(60*5);
		pswCookie.setMaxAge(60*5);
		response.addCookie(nameCookie);
		response.addCookie(pswCookie);

	%>
    <p>读取cookie数据</p>
	<%
		Cookie[] cookies = request.getCookies();
		if(cookies != null){
			%>
			<table class="table table-striped table-bordered">
				<tr>
					<th>name</th>
					<th>value</th>
				</tr>
				<%
				for(Cookie cookie : cookies){
					String cname = cookie.getName();
					String cvalue = cookie.getValue();
					cvalue = URLDecoder.decode(cvalue, "utf-8");
					%>
					<tr>
					 <td><%=cname%></td>
					 <td><%=cvalue%></td>
					</tr>
					<%
				}
				%>
			</table>
			<%
		}
	%>
</body>
</html>